﻿/* 
    require: Utilities.js
*/
etui.ui = etui.ui || {};
!function (ns) {
    var defaults = {
        SdkUri: window.location.protocol + '//connect.facebook.net/en_US/all.js',
        appId: "114688978554228",
        status: true,
        cookie: true,
        xfbml: true
        // channelUrl: "http://WWW.MYDOMAIN.COM/channel.html",
        // oauth: true
    };
    //
    // facebook authentication parameters
    // these parameters are for the FB.init function 
    //
    var facebookInitParameters = ["appId", "status", "cookie", "xfbml", "channelUrl", "oauth"];
    var facebookApis = {

};
FacebookSdk.facebookInitParameters = facebookInitParameters;

function FacebookSdk(options) {
    var facebookInitObj = {};
    var url = new etui.util.Url(location.href);
    var sdkUri;
    if (options) {
        for (var ndx = 0, len = facebookInitParameters.length; ndx < len; ndx++) {
            if (options[facebookInitParameters[ndx]]) {
                facebookInitObj[facebookInitParameters[ndx]] = options[facebookInitParameters[ndx]];
            }
            else if (defaults[facebookInitParameters[ndx]]) {
                facebookInitObj[facebookInitParameters[ndx]] = defaults[facebookInitParameters[ndx]];
            }
        }
        options.SdkUri && (sdkUri = options.SdkUri);
    }
    else {
        facebookInitObj = defaults;
    }

    this.facebookSdkUri = sdkUri || defaults.SdkUri;
    this.action = url.param("ac");
    this.nextPage = url.param("np");
    this.receiverName = url.param("rn");
    this.facebookInitObj = facebookInitObj;
    this.url = url;
}
function createNamedElement(tagName, name) {
    var ieTest = /MSIE (\d+)/ig;
    var match = ieTest.exec(navigator.userAgent);
    if (match && parseInt(match[1]) < 8) {
        arguments.callee = function (tagName, name) {
            var ele;
            ele = document.createElement("<" + tagName + " name=\"" + name + "\">");
            return ele;
        }
    }
    else {
        arguments.callee = function (tagName, name) {
            var ele;
            ele = document.createElement(tagName);
            ele.name = name;
            return ele;
        }
    }
    return arguments.callee(tagName, name);
}
//
// posting data to action page
//
function postResult(nextPage, actionName, receiverName, result) {
    var fbroot = document.getElementById("fb-root");
    var iframe = createNamedElement("iframe", "postback");
    iframe.style.display = "none";

    var form = document.createElement("form");
    form.target = "postback";
    form.action = nextPage;
    form.method = "post";

    var ac = createNamedElement("input", "actionName");
    ac.type = "hidden";
    ac.value = actionName;

    var rn = createNamedElement("input", "receiverName");
    rn.type = "hidden";
    rn.value = receiverName;

    var rd = createNamedElement("input", "resultData");
    rd.type = "hidden";
    rd.value = JSON.stringify(result);

    var extProviderC = createNamedElement("input", "externalProviderCode");
    extProviderC.type = "hidden";
    extProviderC.value = "FB";

    fbroot.appendChild(iframe);
    fbroot.appendChild(form);
    form.appendChild(ac);
    form.appendChild(rn);
    form.appendChild(rd);
    form.appendChild(extProviderC);

    form.submit();
}
function getLoginStatus(callback) {
    var userSession;

    FB.getLoginStatus(function (response) {
        if (Object.prototype.toString.call(callback) === "[object Function]") {
            callback(response.session);
        }
    });

}
FacebookSdk.prototype.loadFacebookSdk = function (async, callback) {
    var self = this;
    if (Object.prototype.toString.call(async) === "[object Function]") {
        callback = async;
        async = false;
    }
    function facebookSdkLoaded() {
        var fbroot;
        if ((fbroot = document.getElementById('fb-root'))) {
            FB.init(self.facebookInitObj);
            callback.call();
        }
        else if (!facebookSdkLoaded.hasRun && !async) {
            facebookSdkLoaded.hasRun = 1;
            window.onload = facebookSdkLoaded;
        }
        else {
            throw "cannot find fb-root in doucment";
        }
    }
    window.facebookSdkLoaded = facebookSdkLoaded;
    if (!async) {
        document.write("<script src='" + this.facebookSdkUri + "'></" + "script>");
        document.write("<script>facebookSdkLoaded();</" + "script>");
    }
    else {
        window.fbAsyncInit = function () {
            facebookSdkLoaded();
        };
        (function () {
            var e = document.createElement('script');
            e.async = true;
            e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
            document.getElementsByTagName('head')[0].appendChild(e);
        } ());
    }
};
FacebookSdk.prototype.doAction = function (actionName) {

    actionName = actionName || this.action;
    if (Object.prototype.toString.call(this[actionName]) === "[object Function]") {
        this[actionName]();
    }
};
FacebookSdk.prototype.login = function () {
    var self = this;
    getLoginStatus(function (session) {
        postResult(self.nextPage, self.action, self.receiverName, session);
    });
};
FacebookSdk.prototype.autoLogin = function () {
    this.login();
};
FacebookSdk.prototype.fetchInfo = function () {
    var self = this;
    var fetchUrl = "/me";
    var fields = this.url.param("fields");
    if (fields) {
        fetchUrl += "?fields=" + fields;
    }
    getLoginStatus(function (session) {
        FB.api(fetchUrl, function (response) {
            postResult(self.nextPage, self.action, self.receiverName, response);
        });
    });
};

ns.FacebookSdk = FacebookSdk;
} (etui.ui);